package com.moodbox.server.ai.controller;

import com.moodbox.server.ai.service.AiAnalysisService;
import com.moodbox.server.mood.entity.Mood;
import com.moodbox.server.mood.service.MoodService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class AiAnalysisController {

    private final AiAnalysisService aiAnalysisService;
    private final MoodService moodService;

    @PostMapping("/analyze")
    public Object analyzeMood(
        @RequestParam String text,
        @RequestParam(required = false) MultipartFile image,
        @RequestParam(required = false) MultipartFile audio
    ) {
        log.info("AI分析心情，文字内容：{}", text);
        // TODO: 实现AI情绪分析逻辑
        return null;
    }

    @GetMapping("/week-report")
    public Object getWeekReport() {
        // TODO: 获取一周情绪报告
        return null;
    }
    
    /**
     * 获取最新的AI分析报告
     * @return 最新的AI分析报告
     */
    @GetMapping("/report/latest")
    public ResponseEntity<Map<String, Object>> getLatestReport(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        log.info("获取用户{}的最新AI分析报告", userId);
        
        // 获取用户最新的心情记录
        Mood latestMood = moodService.getTodayMood(userId);
        
        Map<String, Object> response = new HashMap<>();
        
        if (latestMood != null) {
            response.put("reportContent", latestMood.getAiAnalysis());
            response.put("createTime", latestMood.getCreateTime());
            response.put("emotionTag", latestMood.getEmotionTag());
            response.put("emotionIntensity", latestMood.getEmotionIntensity());
        } else {
            // 如果没有最新记录，返回默认信息
            response.put("reportContent", "暂无心情记录，无法生成分析报告。记录你的第一个心情，AI将为你生成专属分析。");
            response.put("createTime", null);
            response.put("emotionTag", null);
            response.put("emotionIntensity", null);
        }
        
        return ResponseEntity.ok(response);
    }
} 